/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.seguridad;

import Model.Parametro;
import Presenter.EnvioPedidoPresenter;
import Presenter.ParametroPresenter;
import Principals.Validadores;
import View.components.Index;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author walter
 */
public class Frm_BuscarVariableSistema extends javax.swing.JDesktopPane {

    private DefaultTableModel mdlBuscarVariable;
    Validadores val = new Validadores();
    
    /**
     * Creates new form Frm_BuscarVariableSistema
     */
    public Frm_BuscarVariableSistema() {
        EnvioPedidoPresenter.actualizarEnvioPedido() ;
        initComponents();
        val.desactivarAccionPegar(txtNombre);
        
        tblBuscarVariable.getTableHeader().setReorderingAllowed(false);

        mdlBuscarVariable = new DefaultTableModel();
        String nombre = txtNombre.getText();
        List<Parametro> lstParametro;
        lstParametro = ParametroPresenter.buscarParametro(nombre);

        mdlBuscarVariable.setNumRows(0);
        mdlBuscarVariable = (DefaultTableModel) tblBuscarVariable.getModel();

        for (int i = 0; i < lstParametro.size(); i++) {
            Parametro p = lstParametro.get(i);
            Object[] row = new Object[]{p.getIdParametro(), p.getNombre(), p.getValorNumerico(), p.getValorString()};
            mdlBuscarVariable.insertRow(i, row);
        }

        tblBuscarVariable.setRowSelectionAllowed(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar2 = new javax.swing.JToolBar();
        btnToolNuevo = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnToolLimpar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        pnlBuscarEmpleado = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JToggleButton();
        txtNombre = new javax.swing.JTextField();
        pnlResultadosEmpleado = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBuscarVariable = new javax.swing.JTable();

        jToolBar2.setFloatable(false);
        jToolBar2.setBorderPainted(false);

        btnToolNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo.setEnabled(false);
        btnToolNuevo.setFocusable(false);
        btnToolNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevoActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolNuevo);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolModificar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setEnabled(false);
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolEliminar);
        jToolBar2.add(jSeparator4);

        btnToolLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpar.setFocusable(false);
        btnToolLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolLimparActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolLimpar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setEnabled(false);
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolBuscar);
        jToolBar2.add(jSeparator3);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setToolTipText("");
        btnToolCancelar.setEnabled(false);
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolCancelar);

        pnlBuscarEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda"));

        lblNombre.setText("Nombre:");

        btnBuscar.setText("Buscar");
        btnBuscar.setAutoscrolls(true);
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarEmpleadoLayout = new javax.swing.GroupLayout(pnlBuscarEmpleado);
        pnlBuscarEmpleado.setLayout(pnlBuscarEmpleadoLayout);
        pnlBuscarEmpleadoLayout.setHorizontalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNombre)
                .addGap(18, 18, 18)
                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(btnBuscar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlBuscarEmpleadoLayout.setVerticalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlResultadosEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));

        tblBuscarVariable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idVariable", "Nombre", "Valor Numérico", "Valor String"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblBuscarVariable);

        javax.swing.GroupLayout pnlResultadosEmpleadoLayout = new javax.swing.GroupLayout(pnlResultadosEmpleado);
        pnlResultadosEmpleado.setLayout(pnlResultadosEmpleadoLayout);
        pnlResultadosEmpleadoLayout.setHorizontalGroup(
            pnlResultadosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultadosEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );
        pnlResultadosEmpleadoLayout.setVerticalGroup(
            pnlResultadosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultadosEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pnlResultadosEmpleado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlBuscarEmpleado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlBuscarEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlResultadosEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void btnToolNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevoActionPerformed
    }//GEN-LAST:event_btnToolNuevoActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
        int i = this.tblBuscarVariable.getSelectedRow();
        if (i != -1) {
            int idVariable = Integer.parseInt(this.tblBuscarVariable.getValueAt(i, 0).toString());
            cambiarVista(new Frm_ModificarVariableSistema(idVariable));
        } else {
        }
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        cambiarVista(new Frm_BuscarVariableSistema());
    }//GEN-LAST:event_btnToolCancelarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String nombre = txtNombre.getText();
        List<Parametro> lstParametro;
        lstParametro = ParametroPresenter.buscarParametro(nombre);
        mdlBuscarVariable.setNumRows(0);
        mdlBuscarVariable = (DefaultTableModel) tblBuscarVariable.getModel();

        for (int i = 0; i < lstParametro.size(); i++) {
            Parametro p = lstParametro.get(i);
            Object[] row = new Object[]{p.getIdParametro(), p.getNombre(), p.getValorNumerico(), p.getValorString()};
            mdlBuscarVariable.insertRow(i, row);
        }
        tblBuscarVariable.setVisible(true);
        JOptionPane.showMessageDialog(this, "Se encontraron " + mdlBuscarVariable.getRowCount() + " coincidencias");
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnToolLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolLimparActionPerformed
        mdlBuscarVariable.setNumRows(0);
        mdlBuscarVariable = (DefaultTableModel) tblBuscarVariable.getModel();
        limpiar();
    }//GEN-LAST:event_btnToolLimparActionPerformed

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        val.validaCaracteresRaros(evt);
    }//GEN-LAST:event_txtNombreKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnBuscar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolLimpar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JPanel pnlBuscarEmpleado;
    private javax.swing.JPanel pnlResultadosEmpleado;
    private javax.swing.JTable tblBuscarVariable;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

    private void limpiar() {
        txtNombre.setText("");
    }
}
